angular.module('Controller')
.controller('WFResourceManageEditCtrl',($scope,$localStorage,NgTableParams,commonUtil,cryptUtil,wfAlert,commonConstant,$http, $uibModalInstance, items)->
    type=items.type
    $scope.data=items.data
    if type=='edit'
        $scope.data.isLeaf=if $scope.data.isLeaf then 1 else 0
        $scope.data.enabled=if $scope.data.enabled then 1 else 0
    $scope.modalTitle=if type=='new' then '新建资源' else '编辑资源'
    $scope.modalEnableList=commonConstant.enabledList()
    $scope.modalResourceTypeList=commonConstant.resourceTypeList()
    $scope.modalMethodTypeList=commonConstant.methodTypeList()
    $scope.newData=()->
        postData={
            accountId:$localStorage['accountId']
            resource:$scope.data.resource
            method:$scope.data.method
            path:$scope.data.path
            description:$scope.data.description
            type:$scope.data.type
            enabled:$scope.data.enabled
        }
#        getParams=commonUtil.transferGetParam(postData)
        $http.post('@ostengar/resource',postData)
        .success((resp)->
            if resp.code==0
                wfAlert.toastInfo('资源创建成功')
            else
                wfAlert.toastError('资源创建失败：',resp.message)
            $uibModalInstance.close('done')
        )

    $scope.editData=()->
        postData={
            accountId:$localStorage['accountId']
            resource:$scope.data.resource
            method:$scope.data.method
            path:$scope.data.path
            description:$scope.data.description
            type:$scope.data.type
            enabled:$scope.data.enabled
        }
        $http.put('/resource/@ostengar'+$scope.data.id,postData)
        .success((resp)->
            if(resp.code==0)
                wfAlert.toastInfo('编辑资源成功')
                console.log('编辑资源：',resp)
                $uibModalInstance.close('done')
            else
                wfAlert.toastError('编辑资源失败：'+resp.message)
        )
    $scope.confirm = ()->
        if type=='new'
            $scope.newData()
        else
            $scope.editData()
    $scope.cancel =()->
        $uibModalInstance.dismiss('cancel')

)